/**
 * Created by chang on 2017/5/25.
 */
import { deepCopy } from '../assets/utils/util'

const state = {
  menuList: [],
  userNavList: []
}
const defaultValue = deepCopy(state)

const getters = {
  menuList: state => {
    return state.menuList
  },
  userNavList: state => {
    return state.userNavList
  }
}
const actions = {}

const mutations = {
  // 初始化
  MENU_INIT () {
    window.$globalHub.$store.state.menus = defaultValue
  },
  // 存储标签
  ADD_MENU: (state, val) => {
    if (!state.menuList.find(x => x.path === val.path)) {
      let item = {}
      state.userNavList.forEach(y => {
        if (y.sub && y.sub.find(z => z.link === val.path)) {
          item = y.sub.find(z => z.link === val.path)
        } else if (y.link === val.path) {
          item = y
        }
      })
      if (item) {
        state.menuList.push({name: item.label, path: item.link})
      }
    }
  },
  // 存储用户菜单权限
  SET_USERNAV: (state, val) => {
    state.userNavList = val
  },
  REMOVE_MENU (state, path) {
    state.menuList = state.menuList.filter(x => x.path !== path)
  }
}
export default {
  state,
  getters,
  mutations,
  actions
}
